package com.lkd.web.controller;

import com.lkd.entity.Users;
import com.lkd.mapper.UsersMapper;
import com.lkd.service.IUserservice;
import com.lkd.service.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserservice userService;

    @Autowired
    private UsersMapper usersMapper;

//用户登录
    @PostMapping("/login")
    public ResponseResult login(@RequestBody Users arg) {
        // 从数据库里面去验证前段项目传过来的账号信息是否存在？
        // controller(控制层)->service（业务层）->mapper（数据持久层）
        Users users = userService.login(arg);
        ResponseResult result = null;
        if (Objects.isNull(users)) {
            result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "登录失败");
            System.out.println("登录失败");
        } else {
            result = new ResponseResult<>(HttpStatus.OK.value(), "登录成功", users);
            System.out.println("登录成功");
        }
        return result; // 最终@RestController注解会把该方法的返回值转换成json对象
    }

//用户注册(增)
    @PostMapping("/addUser")
    public ResponseResult addUser(@RequestBody Users arg){
        Users users = userService.checkUser(arg);
        ResponseResult result = null;
        if ( arg.getAccountId().equals("")){
            System.out.println("输入不可以为空");
            result = new ResponseResult<>(HttpStatus.FORBIDDEN.value(), "输入不可以为空");
        }else {
            if (Objects.isNull(users)) {
                userService.addUser(arg);
                System.out.println("注册成功");
                result = new ResponseResult<>(HttpStatus.OK.value(), "注册成功", arg);
            } else {
                System.out.println("重复注册");
                result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "重复注册");
            }
        }
        return result;
    }

//用户删除(删)
    @PostMapping("/deleteUser")
    public ResponseResult deleteUser(@RequestBody Users arg){
        Users users = userService.checkUser(arg);
        ResponseResult result = null;
        if (Objects.isNull(users)){
            System.out.println("没有该用户");
            result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "没有该用户");
        }else {
            userService.deleteUser(users);
            System.out.println("注销成功");
            result = new ResponseResult<>(HttpStatus.OK.value(), "注销成功", users);
        }
        return result;
    }

//修改用户(改)
    @PostMapping("updateUser")
    public ResponseResult updateUser(@RequestBody Users arg){
        Users users = userService.checkUser(arg);
        ResponseResult result =null;
        if ( userService.updateUser(arg)==1){
            System.out.println("修改成功");
            result = new ResponseResult<>(HttpStatus.OK.value(), "修改成功", users);
        }else {
            System.out.println("修改失败");
            result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "修改失败");
        }
        return result;
    }


//通过accountId查询用户列表(查)
    @GetMapping("/usersList")
    public @ResponseBody Users usersList(Long accountId){
        return usersMapper.selectByPrimaryKey(accountId);
        //http://localhost:8001/users/usersList?accountId=1
    }

}